package com.liu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liu.dao.ComplaintDao;
import com.liu.dto.ComplaintDto;
import com.liu.entity.Complaint;
import com.liu.service.intf.ComplaintService;
import com.liu.vo.PageBean;
import com.liu.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;


@Service
public class ComplaintServiceImpl implements ComplaintService {

    @Autowired
    private ComplaintDao dao;

    @Override
    public R save(Complaint complaint) {
        //执行新增操作
        if(dao.add(complaint)>0){
            //成功
            return R.ok();
        }else{
            //失败
            return R.fail();
        }
    }

    @Override
    public PageBean queryPage(int page, int limit) {
        //1.分页
        PageHelper.startPage(page, limit);
        //2.获取分页结果
        PageInfo<ComplaintDto> pageInfo=new PageInfo<>(dao.selectAll());
        //3.返回分页的数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R del(int id) {
        //1.验证参数
        if(id>0){
            //2.操作数据库 验证是否成功
            if(dao.del(id)>0){
                //删除成功
                return R.ok();
            }else{
                return R.fail();
            }
        }else{
            return R.fail();
        }
    }


    @Override
    public R update(Complaint complaint) {
        return null;
    }

    @Override
    public PageBean queryPageWhere(String info, int page, int limit) {

        if(!StringUtils.hasLength(info) ){
            info= null;
        }else{
            info="%"+info+"%";//包含  模糊查询
        }
        //2.分页
        PageHelper.startPage(page, limit);
        //3.获取分页结果
        PageInfo<ComplaintDto> pageInfo=new PageInfo<>(dao.selectWhere(info));
        //4.返回分页的数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R delBatch(int[] ids) {
        //1.验证参数
        if(ids!=null && ids.length>0){
            //2.执行
            if(dao.delBatch(ids)>0){
                return R.ok();
            }
        }
        return R.fail();
    }


    @Override
    public R login(Complaint notice, HttpSession session) {
        return null;
    }
}


